//
//  JLHeadlineController.m
//  LIangeNews
//
//  Created by sun on 16/1/17.
//  Copyright © 2016年 sun. All rights reserved.
//

#import "JLHeadlineController.h"
#import "JLHeadline.h"
#import "JLHeadlineCell.h"
#import "JLNewsDetailController.h"
@interface JLHeadlineController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic)NSArray * headlines;

@end

@implementation JLHeadlineController
static NSString * const ID = @"headline";
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.showsVerticalScrollIndicator = NO;
    [self loadheadlines];
}
-(void)loadheadlines{
   
    [JLHeadline headlineWithUrlString:self.urlString finished:^(id responseObject, NSError *error) {
        if (responseObject) {
            self.headlines = responseObject;
            [self.tableView reloadData];
        }else{
            NSLog(@"%@",error);
        }
    }];
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.headlines.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JLHeadlineCell *cell = [tableView dequeueReusableCellWithIdentifier:[JLHeadlineCell headlineCellWithIdentifier:self.headlines[indexPath.row]] forIndexPath:indexPath];
    cell.headline = self.headlines[indexPath.row];
    return cell;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSUInteger row = self.tableView.indexPathForSelectedRow.row;
    JLNewsDetailController * detailVC = segue.destinationViewController;
    detailVC.headline = self.headlines[row];
}
#pragma mark /*********************** tableView的代理方法 ***********************/
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    JLHeadline * headline = self.headlines[indexPath.row];
    if (headline.isBigImage) return 180;
    if (headline.imgextra.count == 2) return 120;
    return 80;
    
}

@end
